package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.WarrantyDao;
import com.hdaccp.xingmu.entity.Warranty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/warranty")
public class WarrantyCon {

    @Autowired
    private WarrantyDao warrantyDao;


    @PersistenceContext
    private EntityManager entityManager ;

    @RequestMapping("/Qtiaozhuan")
    public String Qtiaozhuan(){
        return "WEB-INF/jsp/shequfuwu/baoxiubiao";
    }

    //查询 条件查询
    @RequestMapping("/chaxunquanbu")
    @ResponseBody
    public Map<String ,Object> quanbujinayi(int pageSize, int pageNumber,String type) {

        String jpal="FROM Warranty WHERE 1=1";
        if(type!=null &&!type.equals(" ") ){
            jpal=jpal+" and type=" + "'" + type +"'";
        }

        Query qu = entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);

        //查询多少条数据
        String jpa = "select count(m) from Warranty m";

        Query q = entityManager.createQuery(jpa);

        Long count = (Long) q.getSingleResult();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", qu.getResultList());
        return map;
    }


    //修改数据
    @RequestMapping("Qxiugai")
    @ResponseBody
    public Map<String ,Object> Qupd(int id,String doornumber,String name,
    String phone,String order,java.sql.Date  date,float wmoney,float rmoney,int type,String wname,String neirong,String text,int pageNumber){
        Warranty warranty=new Warranty();
        warranty.setId(id);
        warranty.setDoornumber(doornumber);
        warranty.setName(name);
        warranty.setPhone(phone);
        warranty.setOrder(order);
        warranty.setDate(date);
        warranty.setWmoney(wmoney);
        warranty.setRmoney(rmoney);
        warranty.setType(type);
        warranty.setWname(wname);
        warranty.setNeirong(neirong);
        warranty.setText(text);
        Warranty warranty1=warrantyDao.save(warranty);

        return quanbujinayi(15,pageNumber,null);

    }
    //添加建议信息
    @RequestMapping("/Qtianjia")
    @ResponseBody
    public Map<String ,Object> add2(Warranty warranty,int pageNumber,int pageSize){
        Warranty warranty1=new Warranty();
        warranty1.setDoornumber(warranty.getDoornumber());
        warranty1.setText(warranty.getText());
        warranty1.setNeirong(warranty.getNeirong());
        warranty1.setWname(warranty.getWname());
        warranty1.setType(warranty.getType());
        warranty1.setRmoney(warranty.getRmoney());
        warranty1.setWmoney(warranty.getWmoney());
        warranty1.setDate(warranty.getDate());
        warranty1.setOrder(warranty.getOrder());
        warranty1.setPhone(warranty.getPhone());
        warranty1.setName(warranty.getName());
        warrantyDao.save(warranty1);
        return quanbujinayi(pageSize,pageNumber,null);
    }
    //删除数据
    @RequestMapping("/Qdel")
    @ResponseBody
    public Map<String ,Object> Qdel(long id,int pageSize,int pageNumber){
        warrantyDao.deleteById(id);
        return quanbujinayi(pageSize,pageNumber,null);
    }



}
